# limit the number of cpus used by high performance libraries

import pathlib
import json
from track import Tracker
import pandas as pd
import torch.backends.cudnn as cudnn
import torch
import sys
import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
os.environ["OMP_NUM_THREADS"] = "1"
os.environ["OPENBLAS_NUM_THREADS"] = "1"
os.environ["MKL_NUM_THREADS"] = "1"
os.environ["VECLIB_MAXIMUM_THREADS"] = "1"
os.environ["NUMEXPR_NUM_THREADS"] = "1"

lib_path = os.path.abspath(os.path.join('infrastructure', 'yolov5'))
sys.path.append(lib_path)


def init():
    model = Tracker(config_path="/project/train/track-car/car_track/settings/config.yml")
    return model


def process_video(handle=None, input_video=None, args=None, ** kwargs):
    args = json.loads(args)
    # frames_dict = {}
    # for frame in pathlib.Path(input_video).glob('*.png'):
    #     frame_id = int(frame.with_suffix('').name)
    # frames_dict[frame_id] = frame.as_posix()
    # frames = list(frames_dict.items())  # frames[¨] = (frame_id, frame_file)
    # frame_count = len(frames)
    print(input_video,output_tracker_file)
    output_tracker_file = args['output_tracker_file']
    
    tracker.detect(input_video, output_tracker_file)
    return json.dumps({"model_data": {"objects": []}, "status": "success"})


if __name__ == '__main__':
    tracker = init()
    
    with torch.no_grad():
        tracker.detect("/project/train/track-car/car_track/wt/cut3.avi", "b.txt")
